/**
** 其他组件的实例化
**/
define(['../model/data','./service',
        '../plugins/range','select3'],function(DATA,http,range,select3){
    function dealAjax(url,para,idKey,textKey){
        return {
             ajax:http.s2AJAX(url,para,{idkey:idKey,textkey:textKey}),
              minimumInputLength: 1,
              multiple:true
        };
    }

    /**
    **ty=1 表示新建
    **/
    return function(id,node,ty){
        var _initData;
        if(ty) {
            //每次点击更多类别重新加载新项：初始为空
            DATA.addItem(id);
            _initData = DATA.nowTemp[id][0];
        } else {
            //页面刷新 使用模板
            _initData = DATA.temp[id][0];
        }
        switch(id){
            case "Languages":
                    node.find("select.ext-cond").select2(
                        dealAjax("/search/searchAdlocale",
                                 {type:"adlocale",locale:I18N.language},"key","name")
                    ).on("select2:select",function(e){
                        var data = {
                            id: e.params.data.id,
                            name:e.params.data.name
                        }
                      //  e.params.data
                        DATA.update(id,0,data);
                    }).on("select2:unselect",function(e){
                        var idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                        if(idx!=-1) DATA.delItem(id,idx);
                    });
                kit.setTags(node.find("select"),_initData);
                break;
            case "Industries"://工作-行业
                node.find("select.ext-cond").empty().select2({
                    data:$.map(DATA.others.industries,function(item){
                        item.text = item.name;
                        return item;
                    })
                }).on("select2:select",function(e){
                    console.log(e)
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){

                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(_initData));
                break;
            case "EthnicAffinity"://族裔
                node.find("select.ext-cond").empty().select2({
                    data:$.map(DATA.others.ethnicAffinity,function(item){
                        item.text = item.name;
                        return item;
                    })
                }).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(_initData));
                break;
            case "Generation"://世代
                node.find("select.ext-cond").empty().select2({
                    data:$.map(DATA.others.generation,function(item){
                        item.text = item.name;
                        return item;
                    })
                }).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(_initData));
                break;
            case "FamilyStatuses"://父母（所有）
                node.find("select.ext-cond").empty().select2({
                    data:$.map(DATA.others.familyStatuses,function(item){
                        item.id = item.id;
                        item.text = item.name;
                        return item;
                    })
                }).off("select2:select").on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(_initData));
                break;
            case "Politics"://政治（美国）
                node.find("select.ext-cond").empty().select2({
                    data:$.map(DATA.others.politics,function(item){
                        item.text = item.name;
                        return item;
                    })
                }).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(_initData));
                break;
            case "LifeEvents"://生活纪事
                node.find("select").replaceWith('<input type="text" class="lifeEvents-style" style="width:82%;height:35px;">');
                DATA.delItem(id); //删除数据

                var LifeEventsData = kit.filterSelect3Data(DATA.others.lifeEvents,_initData);
                DATA.nowTemp[id][0] = LifeEventsData;

                var theNode = node.find("input[type].lifeEvents-style");
                theNode.select3({
                    placeholder: I18N.getText('createAd', 'selectLifeEvent'),
                    treeData:DATA.others.lifeEvents,
                    data:kit.ids(LifeEventsData)
                });
                theNode.on("select3:select",function(e,a){
                    if(a && a.length > 0){
                        DATA.nowTemp[id][0] = a;
                    } else {
                        DATA.nowTemp[id][0] = []
                    }
                })
                break;
            case "Behavior"://行为
                node.find("select").replaceWith('<input type="text" class="behavior-style" style="width:82%;height:35px;">');
                var theNode = node.find("input[type].behavior-style");
                theNode.select3({
                    placeholder: I18N.getText('createAd', 'selectBehavior'),
                    treeData:DATA.behav,
                    data:kit.ids(_initData)
                });

                theNode.on("select3:select",function(e,a){
                   if(a && a.length > 0){
                       DATA.nowTemp[id][0] = a;
                   } else {
                       DATA.nowTemp[id][0] = []
                   }
                })

                break;
            case "CollegeYears"://大学就读年份
                node.find("select").replaceWith("<div class='year-range'></div>");
                node.find("div.year-range").ranger({
                    min:1980,
                    max:2019,
                    lefVal:(_initData && _initData.start)||null,
                    rigVal:(_initData && _initData.end)||null
                }).on("change",function(e,o){
                    DATA.nowTemp[id][0].start = o.min;
                    DATA.nowTemp[id][0].end = o.max;
                });
                break;
            case "EducationStatuses"://教育程度
                node.find("select.ext-cond").empty().select2({
                    data:DATA.school
                }).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(_initData));
                break;
            case "EducationMajors"://研究领域
                node.find("select").empty().select2(
                        dealAjax("/search/searchAdeducationmajor",
                                 {"type":"adeducationmajor","locale":I18N.language},"id","name")
                ).on("select2:select",function(e){
                        var data = {
                            id: e.params.data.id,
                            name:e.params.data.name
                        }
                        DATA.update(id,0,data);
                    }).on("select2:unselect",function(e){
                        idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                        if(idx!=-1) DATA.delItem(id,idx);
                    });
                kit.setTags(node.find("select"),_initData);
                break;
            case "EducationSchools"://学校
                node.find("select").empty().select2(
                        dealAjax("/search/searchAdeducationschool",
                                 {type:"adeducationschool",locale:I18N.language},"id","name")
                ).on("select2:select",function(e){
                        var data = {
                            id: e.params.data.id,
                            name:e.params.data.name
                        }
                        DATA.update(id,0,data);
                    }).on("select2:unselect",function(e){
                        idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                        if(idx!=-1) DATA.delItem(id,idx);
                    });
                kit.setTags(node.find("select"),_initData);
                break;
            case "NewInterestedIn"://性取向

                DATA.delItem(id); //删除数据
                var NewInterestedInData =  kit.arrayTextToName(_initData);
                DATA.nowTemp[id][0] = NewInterestedInData;
                node.find("select.ext-cond").empty().select2({
                    data:DATA.newints
                }).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                        idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                        if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(NewInterestedInData));
                break;
            case "NewRelationshipStatus"://情感状况
                DATA.delItem(id); //删除数据
                var NewRelationshipStatusData =  kit.arrayTextToName(_initData);
                DATA.nowTemp[id][0] = NewRelationshipStatusData;
                _initData = kit.arrayTextToName(_initData);
                node.find("select.ext-cond").empty().select2({
                    data:DATA.relation
                }).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                        idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                        if(idx!=-1) DATA.delItem(id,idx);
                }).select2("val",kit.ids(NewRelationshipStatusData));
                break;
            case "WorkEmployers"://雇主
                node.find("select").empty().select2(
                        dealAjax("/search/searchAdworkemployer",
                                 {type:"adworkemployer",locale:I18N.language},"id","name")
                ).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                });
                kit.setTags(node.find("select"),_initData);
                break;
            case "WorkPositions":
                node.find("select").empty().select2(
                        dealAjax("/search/searchAdworkposition",
                                 {type:"adworkposition",locale:I18N.language},"id","name")
                ).on("select2:select",function(e){
                    var data = {
                        id: e.params.data.id,
                        name:e.params.data.name
                    }
                    DATA.update(id,0,data);
                }).on("select2:unselect",function(e){
                    idx = kit.objectIn(DATA.nowTemp[id][0],e.params.data,"id");
                    if(idx!=-1) DATA.delItem(id,idx);
                });
                kit.setTags(node.find("select"),_initData);
                break;
            default: node.find("select").select2();

        }

        if(ty) {
            kit.addBorderGreen($('#'+id));
            kit.addBorderGreen1($('#'+id).find('.choices-area'));
            kit.addBorderGreen1($('#'+id).find('.select2-selection--multiple'));
            kit.addBorderGreen1($('#'+id).find('.year-range .dropdown'));

        };
    }
});
